// pages/class/class.js
import profile from '../../profile.js'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lang: app.globalData.language,
    index: profile.class,

    classNav: profile.class.classNav,
    navNum: 0,

    classList: profile.class.classList,
    classList1: profile.class.classList1,

    searchValue: ''
  },

  // 模糊搜索
  unspecificSearchFun: function (val,list,lang,queryList) {
    if (queryList != undefined && queryList != null && queryList.length != 0) {
      for (var i = 0; i < queryList.length; i++) {
        if (lang == 'cn') {
          let num = queryList[i].title.indexOf(val);
          if (num != -1) {
            list.push(queryList[i]);
          }
        }else{
          let num = queryList[i].title_en.indexOf(val);
          if (num != -1) {
            list.push(queryList[i]);
          }
        }
      }
    }
  },

  // 搜索框输入事件
  searchInputValue: function(e) {
    let navNum = this.data.navNum, list = [], val = e.detail.value, lang = app.globalData.language;
    if (navNum == 0){
      let queryList = this.data.classList;
      if (val == '') {
        list = profile.class.classList
      }else{
        this.unspecificSearchFun(val,list,lang,queryList);
      }
      this.setData({
        classList: list
      })
    }else{
      let queryList = this.data.classList1;
      if (val == '') {
        list = profile.class.classList1
      }else{
        this.unspecificSearchFun(val,list,lang,queryList);
      }
      this.setData({
        classList1: list
      })
    }
  },

  // 课程tab切换事件
  changeClassNav: function (e) {
    let id = e.detail.id, list = this.data.classNav;
    for (let i=0;i<list.length;i++){
      if (list[i].id == id) {
        list[i].onthis = true
      }else{
        list[i].onthis = false
      }
    }
    this.setData({
      navNum: id,
      classNav: list,
    })
    profile.class.classNav = list;
  },

  // 语言切换事件
  menuTap: function (e) {
    this.setData({
      lang: e.detail.lang
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '艺术课'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'Art Class'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //console.log(app.globalData.indexClassTabIndex);
    let tabNum = app.globalData.indexClassTabIndex, list = this.data.classNav;
    for (let i=0;i<list.length;i++){
      if (list[i].id == tabNum) {
        list[i].onthis = true
      }else{
        list[i].onthis = false
      }
    }
    this.setData({
      navNum: tabNum,
      classNav: list,
    })

    this.setData({
      lang: app.globalData.language
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '艺术课'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'Art Class'
      })
    }
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
        lang: app.globalData.language
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})